package cn.iocoder.yudao.module.ao.service.classmajor;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.classmajor.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.classmajor.ClassMajorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.classmajor.ClassMajorMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 班级专业关联 Service 实现类
 *
 * @author lfl
 */
@Service
@Validated
public class ClassMajorServiceImpl implements ClassMajorService {

    @Resource
    private ClassMajorMapper classMajorMapper;

    @Override
    public Long createClassMajor(ClassMajorSaveReqVO createReqVO) {
        // 插入
        ClassMajorDO classMajor = BeanUtils.toBean(createReqVO, ClassMajorDO.class);
        classMajorMapper.insert(classMajor);

        // 返回
        return classMajor.getId();
    }

    @Override
    public void updateClassMajor(ClassMajorSaveReqVO updateReqVO) {
        // 校验存在
        validateClassMajorExists(updateReqVO.getId());
        // 更新
        ClassMajorDO updateObj = BeanUtils.toBean(updateReqVO, ClassMajorDO.class);
        classMajorMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassMajor(Long id) {
        // 校验存在
        validateClassMajorExists(id);
        // 删除
        classMajorMapper.deleteById(id);
    }

    @Override
        public void deleteClassMajorListByIds(List<Long> ids) {
        // 删除
        classMajorMapper.deleteByIds(ids);
        }


    private void validateClassMajorExists(Long id) {
        if (classMajorMapper.selectById(id) == null) {
            throw exception(CLASS_MAJOR_NOT_EXISTS);
        }
    }

    @Override
    public ClassMajorDO getClassMajor(Long id) {
        return classMajorMapper.selectById(id);
    }

    @Override
    public PageResult<ClassMajorDO> getClassMajorPage(ClassMajorPageReqVO pageReqVO) {
        return classMajorMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClassMajorImportRespVO importClassMajorList(List<ClassMajorImportExcelVO> importClassMajors, Boolean updateSupport) {
        if (CollUtil.isEmpty(importClassMajors)) {
            throw exception(CLASS_MAJOR_IMPORT_LIST_IS_EMPTY);
        }

        ClassMajorImportRespVO respVO = ClassMajorImportRespVO.builder()
                .createClassMajorCount(0).updateClassMajorCount(0).failureClassMajorCount(0)
                .failureClassMajorMessages(new ArrayList<>()).build();

        // 如果支持更新，先删除所有现有数据
        if (updateSupport) {
            classMajorMapper.delete(null); // 删除所有数据
        }

        // 处理所有导入数据，不进行重复检查
        for (int i = 0; i < importClassMajors.size(); i++) {
            ClassMajorImportExcelVO importClassMajor = importClassMajors.get(i);
            try {
                // 校验数据
                validateClassMajorForImport(importClassMajor);

                // 直接插入数据，不进行重复检查
                ClassMajorDO classMajor = BeanUtils.toBean(importClassMajor, ClassMajorDO.class);
                classMajorMapper.insert(classMajor);
                respVO.setCreateClassMajorCount(respVO.getCreateClassMajorCount() + 1);

            } catch (Exception ex) {
                // 记录失败的数据
                respVO.setFailureClassMajorCount(respVO.getFailureClassMajorCount() + 1);
                respVO.getFailureClassMajorMessages().add(String.format("第 %d 行：%s", i + 1, ex.getMessage()));
            }
        }

        return respVO;
    }

    private void validateClassMajorForImport(ClassMajorImportExcelVO importClassMajor) {
        if (importClassMajor.getClassId() == null) {
            throw new RuntimeException("班级ID不能为空");
        }
        if (importClassMajor.getMajorId() == null) {
            throw new RuntimeException("专业ID不能为空");
        }
        if (importClassMajor.getMajorDirectionId() == null) {
            throw new RuntimeException("专业方向ID不能为空");
        }
    }

}